package com.newcoin.xgb.exception;

import com.newcoin.xgb.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ValidationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 方法参数校验
     *  @NotBlank（message返回格式）
     */
    @ExceptionHandler(BindException.class)
    public Result BindException(BindException e) {
        log.error(e.getMessage());
        return new Result(500, e.getBindingResult().getFieldError().getDefaultMessage(), null);
    }

    @ExceptionHandler(ValidationException.class)
    public Result validationException(ValidationException e) {
        log.error(e.getMessage());
        return new Result(500, e.getMessage(), "");
    }
}
